﻿using Shop.Model.DB;
using Shop.Model.ModuleGoods;
using Shop.RemoteModel.PageModuleGoods.Model;
using Shop.Repository;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Collect.lmpl
{
    internal class PageModuleGoodsCollect : IPageModuleGoodsCollect
    {
        private readonly IPageModuleGoodsRepository _Repository;

        public PageModuleGoodsCollect ( IPageModuleGoodsRepository repository )
        {
            this._Repository = repository;
        }
        public Dictionary<long, int> SetSort ( DBPageModuleGoods template, int sort )
        {
            int num = template.Sort - sort;
            Dictionary<long, int> sorts = new Dictionary<long, int>()
            {
               {template.Id,sort }
            };
            if ( Math.Abs(num) == 1 )
            {
                long id = this._Repository.Get(a => a.TModuleId == template.TModuleId && a.Sort == sort, a => a.Id);
                if ( id != 0 )
                {
                    sorts.Add(id, template.Sort);
                }
            }
            else
            {
                int begin = template.Sort;
                int end = sort;
                if ( begin > end )
                {
                    begin = sort;
                    end = template.Sort - 1;
                }
                else
                {
                    begin += 1;
                }
                var datas = this._Repository.Gets(a => a.TModuleId == template.TModuleId && SqlFunc.Between(a.Sort, begin, end), a => new
                {
                    a.Id,
                    a.Sort
                }).OrderBy(a => a.Sort).ToArray();
                if ( !datas.IsNull() && datas[0].Sort == sort )
                {
                    num = num > 0 ? 1 : -1;
                    for ( int i = 0 ; i < datas.Length ; i++ )
                    {
                        int next = i + 1;
                        var t = datas[i];
                        int s = t.Sort + num;
                        sorts.Add(t.Id, s);
                        if ( next != datas.Length && datas[next].Sort != s )
                        {
                            break;
                        }
                    }
                }
            }
            this._Repository.SetSort(sorts);
            return sorts;
        }
        public DBPageModuleGoods Get ( long id )
        {
            return this._Repository.Get(id);
        }
        public void Delete ( DBPageModuleGoods obj )
        {
            this._Repository.Delete(obj.Id);
        }
        public void Delete ( long[] ids )
        {
            this._Repository.Delete(ids);
        }

        public ModuleGoodsBase[] GetGoods ( long tModuleId, string tag )
        {
            return this._Repository.GetGoods(tModuleId, tag);
        } 
        public long[] Query ( long moduleId, string tag, IBasicPage paging, out int count )
        {
            return _Repository.Query(moduleId, tag, paging, out count);
        }
        public long[] GetGoodsId ( long moduleId, string tag, int top )
        {
            return _Repository.GetGoodsId(moduleId, tag, top);
        }

        public ModuleGoodsBase[] Sync ( ModuleGoodsAdd add )
        {
            long[] ids = this._Repository.GetId(add.TModuleId, add.TagKey);
            if ( ids.IsNull() && add.GoodsId.IsNull() )
            {
                return null;
            }
            else if ( add.GoodsId.IsNull() )
            {
                this._Repository.Delete(ids);
                return null;
            }
            else
            {
                return this._Repository.Sync(add, ids);
            }

        }
        public void Clear ( long moduleId, string[] filter )
        {
            var data = this._Repository.Gets(a => a.TModuleId == moduleId, a => new
            {
                a.Id,
                a.TagKey
            });
            if ( !data.IsNull() )
            {
                long[] ids = data.Where(a => !filter.Contains(a.TagKey)).Select(a => a.Id).ToArray();
                if ( ids.IsNull() )
                {
                    return;
                }
                this._Repository.Delete(ids);
            }
        }
        public void Clear ( long moduleId )
        {
            long[] ids = this._Repository.GetId(moduleId, null);
            if ( !ids.IsNull() )
            {
                this._Repository.Delete(ids);
            }
        }
    }
}
